package app.netant.finishbook.zxcs8;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Zxcs8Detail extends BaseDetail {

	public Zxcs8Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.zxcs8.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select(".wrap #pleft #content h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int st = title.indexOf("《");
			int ed = title.indexOf("》");
			if(st>=0 && ed>=0 && ed>st){
				title = title.substring(st+1,ed);
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title);
		}
	}

	@Override
	public void analyseTags() {
		
		Elements parts = this.select(".wrap .content .plus_box .plus_l ul li");
		for(int i=0;i<parts.size();i++){
			
			String tags = parts.get(i).text();
			
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】："+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
			
		}
		
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".wrap #pleft #content p");
		for(int i=0;i<parts.size();i++){
			String memo = this.getString(parts.get(i).text().trim());
			if(memo.indexOf("内容简介")>0){
				System.out.println("【Memo】:"+memo);
				this.entity.setNovelMemo(memo);
				break;
			}
		}
	}

	@Override
	public void analyseHref() {
		
		Elements parts = this.select(".wrap #pleft #content .pagefujian .down_2 a");
		if(parts.size()>0){
			String href = parts.get(0).attr("href");
			System.out.println("【Url】:"+href);
			
			Zxcs8Detail detail = new Zxcs8Detail(href);
			detail.setEntity(this.entity);
			
			detail.analyseTags();
			
			String url = detail.stepHref();
			System.out.println("【Href】:"+url);
			
			int idx = url.lastIndexOf(".");
			if(idx>0){
				String exp = url.substring(idx);
				this.entity.setNovelFullName(this.entity.getNovelName()+exp);
			}
			
			this.entity.setDownPath(url);
			
		}
		
	}
	
	public String stepHref(){
		Elements parts = this.select(".wrap .content .panel .panel-body .downfile a");
		if(parts.size()>0){
			String href = parts.get(0).attr("href");
			return href;
		}else{
			return null;
		}
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select(".wrap #pleft #ptop a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Zxcs8Detail detail = new Zxcs8Detail("http://www.zxcs8.com/post/11227");
		detail.test();
	}



}
